<!-- <template>
	<button@click="handleLoginTrigger">微信一键登录</button>
</template>

<script>
	import {
		getwxloginMessage,
		decryptPhone
	} from '../request/api.js'

	export default {
		data() {
			return {
				sessionKey: '', // 存储登录后的session_key
				tempCode: '' // 存储临时登录code
			}
		},
		methods: {
			async handleLoginTrigger() {
				try {
					// Step 1: 获取用户基本信息
					const {
						userInfo
					} = await uni.getUserProfile({
						desc: '用于完善会员信息',
						lang: 'zh_CN'
					});

					// Step 2: 获取登录凭证
					const {
						code
					} = await uni.login({
						provider: 'weixin'
					});
					this.tempCode = code;

					// Step 3: 调用后端登录接口
					const {
						openid,
						sessionKey
					} = await getwxloginMessage(code);
					this.sessionKey = sessionKey;

				} catch (err) {
					this.handleError(err);
				}
			},

			async handleGetPhoneNumber(e) {
				if (e.detail.errMsg !== 'getPhoneNumber:ok') return;

				try {
					// Step 4: 获取加密数据
					const {
						encryptedData,
						iv
					} = e.detail;

					var WXBizDataCrypt = require('../common/WXBizDataCrypt.js'); //路径要对应，你新建的WXBizDataCrypt.js文件路径
					const accountInfo = uni.getAccountInfoSync(); //获取小程序appid
					let appid = accountInfo.miniProgram.appId
					var pc = new WXBizDataCrypt(appid, this.sessionKey);
					var data = pc.decryptData(encryptedData, iv);
					console.log(data)
					// Step 6: 合并数据提交到后端
					// const finalRes = await this.$api.completeLogin({
					// 	code: this.tempCode,
					// 	phone: res.phoneNumber
					// });

					uni.showToast({
						title: '登录成功'
					});

				} catch (err) {
					this.handleError(err);
				}
			},

			handleError(err) {
				if (err.errMsg?.includes('deny')) {
					uni.showToast({
						title: '授权已取消',
						icon: 'none'
					});
				} else {
					uni.showToast({
						title: '登录失败，请重试',
						icon: 'none'
					});
					console.error('完整错误信息:', err);
				}
			}
		}
	}
</script> -->